﻿#include "Producer.h"
#include "Resources.h"
#include "Controller.h"
#include <QDebug>
#include <QRandomGenerator>


Producer::Producer(QObject *parent)
    : Runable{parent}
{
    buffer = Controller::getQueue();
}

void Producer::run()
{
    qDebug() << "Producer Running in:" << QThread::currentThreadId();

    static auto random = QRandomGenerator::global();
    static int idx = 0;
    while (isRunning) {
        int begin = random->bounded(100);
        int end = random->bounded(100);
        QString good = QString::asprintf("%d %s %s", ++idx, WORD_TABLE[begin], WORD_TABLE[end]);
        buffer->enqueue(good);
        emit sendMsg("Make [" + good + "]");

        // 时间尽可能小或不设，最大化并发冲突概率
        QThread::msleep(QRandomGenerator::global()->bounded(10));
    }
}

void Producer::slotTID()
{
    qDebug() << "Producer Slots Function In:" << QThread::currentThreadId();
}
